;!function (win) {

    var Admin = function () {

    };
    //分页
    Admin.prototype.paginate = function (count, curr, limit, limits) {
        layui.laypage.render({
            elem: 'page',
            count: count,
            curr: curr,
            limit: limit,
            limits: limits ? limits : [12, 15, 30, 50],
            layout: ['first', 'prev', 'page', 'next', 'last', 'count', 'skip', 'limit'],
            jump: function (obj, first) {
                if (!first) {
                    location.href = window.location.pathname + '?' + $("#search-form").serialize() + '&page=' + obj.curr + '&limit=' + obj.limit;
                }
            }
        });
    };

    //表格删除功能
    Admin.prototype.tableDataDelete = function (url, th, msg) {
        layui.layer.confirm(msg, {
            btn: ['删除', '取消']
        }, function () {
            $.ajax({
                type: "GET",
                url: url,
                success: function (res) {
                    if (res.code > 0) {
                        layui.layer.close();
                        layui.layer.msg(res.msg, {time: 2000, icon: 5});
                        return false;
                    }
                    layui.layer.close();
                    layui.layer.msg(res.msg, {time: 2000, icon: 6});
                    window.location = window.location.href;
                    return false;
                },
            });
        }, function () {
            layui.layer.close();
        });
    };

    //弹窗
    Admin.prototype.openLayerForm = function (url, title, method, width, height, noRefresh, formId) {
        var formId = formId ? formId : "#layer-form";
        $.get(url, function (view) {
            layui.layer.open({
                type: 1,
                title: title,
                anim: 2,
                shadeClose: true,
                content: view,
                success: function () {
                    layui.form.render();
                },
                area: [
                    width ? width : '50%',
                    height ? height : '500px'
                ],
                btn: ['确认', '重置'],
                yes: function (index, layero) {
                    var formObj = $(formId);
                    $.ajax({
                        type: method ? method : 'POST',
                        url: formObj.attr("action"),
                        dataType: "json",
                        data: formObj.serialize(),
                        success: function (response) {
                            // console.log(response)
                            if (response.error > 0) {
                                layui.layer.msg(response.msg, {time: 3000, icon: 5})
                            } else {
                                layui.layer.close(index);
                                layui.layer.msg(response.msg, {time: 2000, icon: 6})
                                if (!noRefresh) {
                                    window.location = window.location.href
                                }
                            }
                        },
                    });
                },
                btn2: function (index, layero) {
                    $(formId)[0].reset();
                    return false;
                }
            });
        });
    };

    //纯弹窗
    Admin.prototype.openLayerShowForm = function (url, title, width, height, formId) {
        var formId = formId ? formId : "layer-form";

        $.get(url, function (view) {
            layui.layer.open({
                type: 1
                , title: title
                , id: formId //防止重复弹出
                , content: view
                , btn: '关闭'
                , btnAlign: 'c' //按钮居中
                , anim: 2,
                shadeClose: true
                , yes: function () {
                    layui.layer.closeAll();
                },
                area: [
                    width ? width : '50%',
                    height ? height : '500px'
                ],
            });
        });
    }


    /**
     * 添加时间：2021-03-18
     * 方法名称：ajax发送信息
     * */
    Admin.prototype.send = function (url, method = "GET", data = {}, flag = 1) {
        $.ajax({
            type: method,
            url: url,
            data: data,
            success: function (res) {
                if (flag == 1) {
                    if (res.error > 0) {
                        layui.layer.msg(res.msg, {time: 2000, icon: 5});
                        return false;
                    }
                    layui.layer.msg(res.msg, {time: 2000, icon: 6});
                    return false;
                }
                if (flag == 2) {
                    if (res.error > 0) {
                        layui.layer.msg(res.msg, {time: 2000, icon: 5});
                        return false;
                    }
                    window.location = window.location.href
                    layui.layer.msg(res.msg, {time: 2000, icon: 6});
                    return false;
                }
            },
        });
    }

    /**
     * 添加时间：2021-03-18
     * 方法名称：ajax发送信息
     * */
    Admin.prototype.sendCallback = function (url, method = "GET", data = {}, callback) {
        $.ajax({
            type: method,
            url: url,
            data: data,
            success: function (res) {
                callback(res);
            },
        });
    }

    /**
     * 操作按钮下拉事件
     * */
    Admin.prototype.buttonGroup = function (html, that, layer) {
        let idx = layer.open({
            type: 4,
            content: [html, that],
            shade: 0,
            skin: 'demo-class',
            tips: [3, '#f0f0f0'],
            closeBtn: 0,
            area: 'auto', maxWidth: 80,
        });
        return idx;
    }

    /**
     * 去除重复值
     * */
    //去重
    Admin.prototype.distinct = function (arr) {
        return Array.from(new Set(arr))
    }

    /**
     * 设置提价数据
     * */
//提交数据
    Admin.prototype.set = function (url, token, name, data) {
        $.ajax({
            url: url,
            data: {"_token": token, "name": name, "value": data},
            method: "post",
            success: function (res) {
                if (res.error > 0) {
                    //错误框
                    layer.msg(res.msg, {
                        anim: 6,
                        icon: 5,
                        time: 3000
                    });
                    return false;
                }
                layer.msg(res.msg, {icon: 6});
            }
        });
    }
    win.admin = new Admin();
}(window);

layui.config({
    base: "/layui/"
});

layui.use("jquery", function () {
    $ = layui.jquery;

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var status = XMLHttpRequest.status;
            var responseText = XMLHttpRequest.responseText;
            var msg = '不好，有错误';
            switch (status) {
                case 400:
                    msg = responseText != '' ? responseText : '失败了';
                    break;
                case 401:
                    msg = responseText != '' ? responseText : '你没有权限';
                    break;
                case 403:
                    msg = '你没有权限执行此操作!';
                    break;
                case 404:
                    msg = '你访问的操作不存在';
                    break;
                case 406:
                    msg = '请求格式不正确';
                    break;
                case 410:
                    msg = '你访问的资源已被删除';
                    break;
                case 422:
                    var errors = $.parseJSON(XMLHttpRequest.responseText);

                    if (errors instanceof Object) {
                        var m = '';
                        $.each(errors, function (index, item) {
                            if (item instanceof Object) {
                                $.each(item, function (index, i) {
                                    m = m + i + '<br>';
                                });
                            } else {
                                m = m + item + '<br>';
                            }
                        });
                        msg = m;
                    }
                    break;
                case 429:
                    msg = '超出访问频率限制';
                    break;
                case 500:
                    msg = '500 INTERNAL SERVER ERROR';
                    break;
                default:
                    return true;
            }

            layer.msg(msg, {time: 3000, icon: 5});
        }
    });
});
